package com.cloud.gateway.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * RouterConfig
 *
 * @author H
 * @version 1.0
 * @date 2023-11-28 17:08
 */
@Configuration
public class RouterConfig {

    /**
     * api式配置路由
     * 默认情况下，转发的完整地址为uri 拼接上我们请求的path
     */
    @Bean
    public RouteLocator jdRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("jd_router", t -> t.path("/jd").uri("https://jd.com"))
                .build();
    }

    /**
     * 自定义过滤器
     */
    @Bean
    public RouteLocator routeFilter(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("fl_router", t -> t.path("/fl")
                        .filters(d -> d.setRequestHeader("X-Request-Id", "a"))
                        .uri("http://10.2.21.17:9001/t"))
                .build();
    }

}
